﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using DAL;

namespace BLL.NguoiDung
{
    public class HoSo
    {
        public static int AddHoSo(int userId, string tieuDe, bool gioiTinh, DateTime ngaySinh, string ttHonNhan,
                                  string quocTich, string urlAnh, string diaChi, int tinhId, string dienthoai,
                                  string didong, string email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoso = new tbl_HS_HoSo
                           {
                               UserId = userId,
                               TieuDe = tieuDe,
                               GioiTinh = gioiTinh,
                               NgaySinh = ngaySinh,
                               TTHonNhan = ttHonNhan,
                               QuocTich = quocTich,
                               UrlAnh = urlAnh,
                               DiaChi = diaChi,
                               TinhId = tinhId,
                               DienThoai = dienthoai,
                               DiDong = didong,
                               Email = email,
                               Step = 1,
                               SoLanXem = 0,
                               HoanThanh = false,
                               KichHoat = false,
                               LastUpdate = DateTime.Now
                           };
            db.tbl_HS_HoSos.InsertOnSubmit(hoso);
            db.SubmitChanges();
            return hoso.HoSoId;
        }

        public static void AddHocVan(int hoSoId, int trinhDoHocVanId, string tenTruong, int chuyenMonId, int quocGiaId,
                                     DateTime ngayBatDau, DateTime ngayKetThuc, string quaTrinhHocVan)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                db.tbl_HS_HocVans.InsertOnSubmit(new tbl_HS_HocVan
                                                     {
                                                         HoSoId = hoSoId,
                                                         TrinhDoHocVanId = trinhDoHocVanId,
                                                         TenTruong = tenTruong,
                                                         ChuyenMonId = chuyenMonId,
                                                         QuocGiaId = quocGiaId,
                                                         NgayBatDau = ngayBatDau,
                                                         NgayKetThuc = ngayKetThuc,
                                                         QuaTrinhHocVan = quaTrinhHocVan,

                                                     });
                hoSo.Step = 1;
                db.SubmitChanges();
            }
        }

        public static void AddNgoaiNgu(int hoSoId, string ngoaiNgu1, int? capdo1, string ngoaiNgu2, int? capdo2,
                                       string ngoaiNgu3, int? capdo3)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                db.tbl_HS_NgoaiNgus.InsertOnSubmit(new tbl_HS_NgoaiNgu
                                                       {
                                                           HoSoId = hoSoId,
                                                           NgoaiNgu1 = ngoaiNgu1,
                                                           CapDo1 = capdo1,
                                                           NgoaiNgu2 = ngoaiNgu2,
                                                           CapDo2 = capdo2,
                                                           NgoaiNgu3 = ngoaiNgu3,
                                                           CapDo3 = capdo3
                                                       });
                hoSo.Step = 2;
                db.SubmitChanges();
            }
        }

        public static void AddKinhNghiemLamViec(int hoSoId, int tongSoNam, string tenCongTy, string chucDanh,
                                                int nganhNgheId, DateTime tuNgay, DateTime denNgay, int quocGiaId,
                                                string mota)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                db.tbl_HS_KinhNghiemLamViecs.InsertOnSubmit(new tbl_HS_KinhNghiemLamViec
                                                                {
                                                                    HoSoId = hoSoId,
                                                                    TongSoNam = tongSoNam,
                                                                    TenCongTy = tenCongTy,
                                                                    ChucDanh = chucDanh,
                                                                    NganhNgheId = nganhNgheId,
                                                                    TuNgay = tuNgay,
                                                                    DenNgay = denNgay,
                                                                    Mota = mota
                                                                });
                hoSo.Step = 3;
                db.SubmitChanges();
            }
        }

        public static void XoaHoSo(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hosoId);
            if (hoSo != null)
            {
                var hocvans = db.tbl_HS_HocVans.Where(r => r.HoSoId == hosoId);

                db.tbl_HS_HocVans.DeleteAllOnSubmit(hocvans);

                var ngoaingu = db.tbl_HS_NgoaiNgus.Where(r => r.HoSoId == hosoId);
                db.tbl_HS_NgoaiNgus.DeleteAllOnSubmit(ngoaingu);

                var kn = db.tbl_HS_KiNangLamViecs.Where(r => r.HoSoId == hosoId);
                db.tbl_HS_KiNangLamViecs.DeleteAllOnSubmit(kn);

                var kn1 = db.tbl_HS_KinhNghiemLamViecs.Where(r => r.HoSoId == hosoId);
                db.tbl_HS_KinhNghiemLamViecs.DeleteAllOnSubmit(kn1);

                var a = db.tbl_HS_MucTieuNgheNghieps.Where(r => r.HoSoId == hosoId);
                db.tbl_HS_MucTieuNgheNghieps.DeleteAllOnSubmit(a);

                var nophoso = db.tbl_TD_NopTuyenDungs.Where(r => r.HoSoId == hosoId);
                db.tbl_TD_NopTuyenDungs.DeleteAllOnSubmit(nophoso);

                db.tbl_HS_HoSos.DeleteOnSubmit(hoSo);
                db.SubmitChanges();

            }
        }

        public static void AddKiNangLamViec(int hoSoId, int nhomKiNangId, int kiNangId, int capdo, int soNamSuDung)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                db.tbl_HS_KiNangLamViecs.InsertOnSubmit(new tbl_HS_KiNangLamViec
                                                            {
                                                                HoSoId = hoSoId,
                                                                NhomKiNangId = nhomKiNangId,
                                                                KiNangId = kiNangId,
                                                                CapDo = capdo,
                                                                SoNamSuDung = soNamSuDung
                                                            });
                hoSo.Step = 4;
                db.SubmitChanges();
            }
        }

        public static void AddMucTieuNgheNghiep(int hoSoId, string viTri, int? luongGanNhat, int luongTu, int luongDen,
                                                int capBacMongMuongId, int loaiCVId, string nganhNghe, string noiLam,
                                                string mucTieu)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                db.tbl_HS_MucTieuNgheNghieps.InsertOnSubmit(new tbl_HS_MucTieuNgheNghiep
                                                                {
                                                                    HoSoId = hoSoId,
                                                                    ViTriMongMuon = viTri,
                                                                    MucLuongGanNhat = luongGanNhat,
                                                                    MucLuongMongMuonTu = luongTu,
                                                                    MucLuongMongMuonTuDen = luongDen,
                                                                    CapBacMongMuonId = capBacMongMuongId,
                                                                    LoaiCongViecId = loaiCVId,
                                                                    NganhNghe = nganhNghe,
                                                                    MoiLamUaThich = noiLam,
                                                                    MucTieu = mucTieu
                                                                });
                hoSo.Step = 5;
                db.SubmitChanges();
            }
        }

        public static void UpdateMucTieuNgheNghiep(int id, string viTri, int? luongGanNhat, int luongTu, int luongDen,
                                                   int capBacMongMuongId, int loaiCVId, string nganhNghe, string noiLam,
                                                   string mucTieu)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_MucTieuNgheNghieps.FirstOrDefault(r => r.Id == id);
            if (hoSo != null)
            {

                hoSo.MucLuongGanNhat = luongGanNhat;
                hoSo.MucLuongMongMuonTu = luongTu;
                hoSo.MucLuongMongMuonTuDen = luongDen;
                hoSo.CapBacMongMuonId = capBacMongMuongId;
                hoSo.LoaiCongViecId = loaiCVId;
                hoSo.NganhNghe = nganhNghe;
                hoSo.MoiLamUaThich = noiLam;
                hoSo.MucTieu = mucTieu;
                db.SubmitChanges();
            }
        }



        public static void PublicHoSo(int hoSoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoSo = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hoSoId);
            if (hoSo != null)
            {
                hoSo.Step = 6;
                hoSo.HoanThanh = true;
                hoSo.SoLanXem = 0;
                db.SubmitChanges();
            }
        }

        public static IQueryable GetHoSoByUserId(int userId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HoSos
                .Where(r => r.UserId == userId).Select(r => new
                                                                {
                                                                    r.HoSoId,
                                                                    r.TieuDe,
                                                                    r.LastUpdate,
                                                                    r.SoLanXem,
                                                                    r.KichHoat,
                                                                    HoanThanh =
                                                                r.HoanThanh ? "Hoàn Thành" : "Chưa hoàn thành"
                                                                });
        }


        public static IQueryable GetHoSoHoanThanh(int userId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HoSos
                .Where(r => r.UserId == userId && r.HoanThanh).Select(r => new
                {
                    r.HoSoId,
                    r.TieuDe,
                    r.LastUpdate,
                    r.SoLanXem,
                    r.KichHoat,
                    HoanThanh =
                r.HoanThanh ? "Hoàn Thành" : "Chưa hoàn thành"
                });
        }

        public static bool Apply(int userId,int hoSoId, int tuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            if(db.tbl_TD_NopTuyenDungs.Count(r=>r.TuyenDungId==tuyenDungId && r.tbl_HS_HoSo.UserId==userId)>0)
            {
                return false;
            }
            var check = db.tbl_TD_NopTuyenDungs.Count(r => r.HoSoId == hoSoId && r.TuyenDungId == tuyenDungId);
            if(check<1)
            {
                db.tbl_TD_NopTuyenDungs.InsertOnSubmit(new tbl_TD_NopTuyenDung
                                                           {
                                                               HoSoId = hoSoId,
                                                               TuyenDungId = tuyenDungId,
                                                               DaDoc = false,
                                                               NgayGui = DateTime.Now
                                                           });
                db.SubmitChanges();

                return true;
            }

            return false;
        }

        public static int CountTotalHoSo(int userId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HoSos.Count(r => r.UserId == userId);
        }

        public static tbl_HS_HoSo LoadHoSo(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hosoId);
        }

        public static IQueryable LoadHocVan(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HocVans
                .Where(r => r.HoSoId == hosoId).Select(r => new
                                                                {
                                                                    r.HocVanId,
                                                                    r.TenTruong,
                                                                    HocVan = r.tbl_DM_HocVan.Mota,
                                                                    ChuyenMon = r.tbl_DM_NganhNghe.Ten,
                                                                    r.NgayKetThuc
                                                                });
        }


        public static IQueryable LoadKinhNghiemLamViec(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_KinhNghiemLamViecs
                .Where(r => r.HoSoId == hosoId).Select(r => new
                                                                {
                                                                    r.Id,
                                                                    r.TenCongTy,
                                                                    r.ChucDanh,
                                                                    r.TuNgay,
                                                                    r.DenNgay,

                                                                });
        }



        public static IQueryable LoadKiNang(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_KiNangLamViecs
                .Where(r => r.HoSoId == hosoId).Select(r => new
                                                                {
                                                                    Capdo = r.CapDo == 1
                                                                                ? "Sơ cấp"
                                                                                : r.CapDo == 2 ? "Trung cấp" : "Cao cấp",
                                                                    r.SoNamSuDung,
                                                                    r.tbl_DM_NhomKiNang.TenNhomKiNang,
                                                                    r.tbl_DM_KiNang.TenKiNang,
                                                                    r.Id
                                                                });
        }


        public static tbl_HS_NgoaiNgu LoadNgoaiNgu(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_NgoaiNgus.FirstOrDefault(r => r.HoSoId == hosoId);
        }

        public static tbl_HS_MucTieuNgheNghiep LoadMucTieu(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_MucTieuNgheNghieps.FirstOrDefault(r => r.HoSoId == hosoId);
        }

        public static void XoaHocVan(int hocvanId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hocvan = db.tbl_HS_HocVans.FirstOrDefault(r => r.HocVanId == hocvanId);
            if (hocvan != null)
            {
                db.tbl_HS_HocVans.DeleteOnSubmit(hocvan);
                db.SubmitChanges();
            }
        }

        public static void XoaKinhNghiemLamViec(int id)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var kn = db.tbl_HS_KinhNghiemLamViecs.FirstOrDefault(r => r.Id == id);
            if (kn != null)
            {
                db.tbl_HS_KinhNghiemLamViecs.DeleteOnSubmit(kn);
                db.SubmitChanges();
            }
        }

        public static void XoaKiNangLamViec(int kiNangId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var kn = db.tbl_HS_KiNangLamViecs.FirstOrDefault(r => r.Id == kiNangId);
            if (kn != null)
            {
                db.tbl_HS_KiNangLamViecs.DeleteOnSubmit(kn);
                db.SubmitChanges();
            }
        }

        public static User LoadUserByHoSoId(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoso = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hosoId);
            if (hoso != null)
            {
                return db.Users.FirstOrDefault(r => r.UserId == hoso.UserId);
            }
            return null;
        }





        public static IQueryable LoadHocVanForView(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");

            return db.tbl_HS_HocVans
                .Where(r => r.HoSoId == hosoId).Select(r => new
                                                                {
                                                                    r.NgayBatDau,
                                                                    r.NgayKetThuc,
                                                                    r.TenTruong,
                                                                    TenChuyenMon = r.tbl_DM_NganhNghe.Ten,
                                                                    TrinhDoHocVan = r.tbl_DM_HocVan.Mota,
                                                                    TenQuocGia = r.tbl_DM_QuocGia.Ten
                                                                });

        }

        public static List<ItemNgoaiNgu> LoadNgoaiNguForView(int hosoId)
        {
            var lst = new List<ItemNgoaiNgu>();
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");

            var ngoaingu = db.tbl_HS_NgoaiNgus.FirstOrDefault(r => r.HoSoId == hosoId);
            if (ngoaingu != null)
            {
                if (ngoaingu.NgoaiNgu1 != null && ngoaingu.CapDo1 != null)
                {

                    var item = new ItemNgoaiNgu
                                   {TenNgoaiNgu = ngoaingu.NgoaiNgu1, CapDo = TenCapDo((int) ngoaingu.CapDo1.Value)};
                    lst.Add(item);
                }

                if (ngoaingu.NgoaiNgu2 != null && ngoaingu.CapDo2 != null)
                {

                    var item = new ItemNgoaiNgu
                                   {TenNgoaiNgu = ngoaingu.NgoaiNgu2, CapDo = TenCapDo((int) ngoaingu.CapDo2.Value)};
                    lst.Add(item);
                }

                if (ngoaingu.NgoaiNgu3 != null && ngoaingu.CapDo3 != null)
                {

                    var item = new ItemNgoaiNgu
                                   {TenNgoaiNgu = ngoaingu.NgoaiNgu3, CapDo = TenCapDo((int) ngoaingu.CapDo3.Value)};
                    lst.Add(item);
                }

            }
            return lst;
        }

        public static IQueryable LoadKiNangForView(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_KiNangLamViecs
                .Where(r => r.HoSoId == hosoId).Select(r => new
                                                                {
                                                                    TenNhomKiNang = r.tbl_DM_NhomKiNang.TenNhomKiNang,
                                                                    r.tbl_DM_KiNang.TenKiNang,
                                                                    Capdo = r.CapDo == 1
                                                                                ? "Sơ cấp"
                                                                                : r.CapDo == 2 ? "Trung cấp" : "Cao cấp",
                                                                    r.SoNamSuDung
                                                                });
        }


        public static int CountTotalExp(int hosoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var kn = db.tbl_HS_KinhNghiemLamViecs.Where(r => r.HoSoId == hosoId);
            int nam = Enumerable.Sum(kn, tblHsKinhNghiemLamViec => tblHsKinhNghiemLamViec.TongSoNam);
            return nam;
        }



        public static string TenCapDo(int id)
        {
            switch (id)
            {
                case 1:
                    return "Sơ cấp";
                case 2:
                    return "Trung cấp";
                case 3:
                    return "Cao cấp";
                case 4:
                    return "Thành thạo";
                case 5:
                    return "Bản ngữ";
                default:
                    return "";
            }

        }



        public static void UpdateNgoaiNgu(int p, string nn1, int p_2, string nn2, int p_3, string nn3, int p_4)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var ngoaingu = db.tbl_HS_NgoaiNgus.FirstOrDefault(r => r.Id == p);
            if (ngoaingu != null)
            {
                ngoaingu.NgoaiNgu1 = nn1;
                ngoaingu.NgoaiNgu2 = nn2;
                ngoaingu.NgoaiNgu3 = nn3;
                ngoaingu.CapDo1 = p_2;
                ngoaingu.CapDo2 = p_3;
                ngoaingu.CapDo3 = p_4;
                db.SubmitChanges();
            }
        }

        public static void UpdateHoSo(int hosoId, string tieuDe, bool gioiTinh, DateTime ngaySinh, string ttHonNhan,
                                      string quocTich, string urlAnh, string diaChi, int tinhId, string dienthoai,
                                      string didong, string email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var hoso = db.tbl_HS_HoSos.FirstOrDefault(r => r.HoSoId == hosoId);
            if (hoso != null)
            {

                hoso.TieuDe = tieuDe;
                hoso.GioiTinh = gioiTinh;
                hoso.NgaySinh = ngaySinh;
                hoso.TTHonNhan = ttHonNhan;
                hoso.QuocTich = quocTich;
                hoso.UrlAnh = urlAnh;
                hoso.DiaChi = diaChi;
                hoso.TinhId = tinhId;
                hoso.DienThoai = dienthoai;
                hoso.DiDong = didong;
                hoso.Email = email;
                hoso.LastUpdate = DateTime.Now;

                db.SubmitChanges();
            }
        }
    }


    public class ItemNgoaiNgu
    {
        public string TenNgoaiNgu { set; get; }
        public string CapDo { set; get; }
    }
}